﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbManagerDal
	{
		public DbManagerDal()
		{
		}
 		#region Insert
 		public static int Insert(DbManager dbManager)
		{	
			string sql = @"Insert Into DbManager (
					MUser,
					MPwd,
					MLastIP,
					MLastDate,
					MRoleCode,
					MStatus,
					MLoginTimes
				) values('{0}','{1}','{2}','{3}',{4},{5},{6})";
			sql = string.Format(sql,
					SqlHelper.String(dbManager.MUser),
					SqlHelper.String(dbManager.MPwd),
					SqlHelper.String(dbManager.MLastIP),
					SqlHelper.String(dbManager.MLastDate),
					dbManager.MRoleCode,
					dbManager.MStatus,
					dbManager.MLoginTimes
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbManager dbManager)
		{	
			string sql = @"Update DbManager set 

					MUser= '{0}',
					MPwd= '{1}',
					MLastIP= '{2}',
					MLastDate= '{3}',
					MRoleCode= {4},
					MStatus= {5},
					MLoginTimes= {6}
				where

					MId= {7}";
			sql = string.Format(sql,
					SqlHelper.String(dbManager.MUser),
					SqlHelper.String(dbManager.MPwd),
					SqlHelper.String(dbManager.MLastIP),
					SqlHelper.String(dbManager.MLastDate),
					dbManager.MRoleCode,
					dbManager.MStatus,
					dbManager.MLoginTimes,
					dbManager.MId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 mId)
		{
		
			string sql = @"Delete from DbManager where 
					MId= {0}";
			sql = string.Format(sql,
					mId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbManager a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbManager Select(Int32 mId)
		{
			DbManager dbManager = null;
			string sql = @"select a.* from DbManager a  where
					a.MId= {0}
";
			sql = string.Format(sql,
					mId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbManager = new DbManager(ds.Tables[0].Rows[0]);					
			return dbManager;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbManager a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbManager a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbManager a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
